//
//  WZDViewController.m
//  QuickCure
//
//  Created by 王忠迪 on 9/13/16.
//  Copyright © 2016 haoge. All rights reserved.
//

#import "WZDViewController.h"
#import <SVProgressHUD.h>

@interface WZDViewController ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate>

@property (nonatomic,weak) UITableView *tableView;

//textView
@property (nonatomic,weak) UITextView *textView;

//placeholder
@property (nonatomic,weak) UILabel *placeholder;

//显示字数的label
@property (nonatomic,weak) UILabel *numLabel;

@end

static NSString *indentifier = @"cell";

@implementation WZDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView = tableView;

    [self.view addSubview:self.tableView];
    
}

#pragma mark -- 实现数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.section == 0)
    {
        [self setCell:cell];
    
    }else if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            cell.imageView.image = [UIImage imageNamed:@"dianhua"];
            
            cell.textLabel.text = @"官服电话: 400-636-6113";
        }
        if (indexPath.row == 1)
        {
            cell.imageView.image = [UIImage imageNamed:@"qq"];
            
            cell.textLabel.text = @"用户交流群: QQ 464768569";
        }
    }
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1)
    {
        return @"别联系我们";
    }
    
    return nil;
}

#pragma mark -- 点击cell的代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"小子,你想好啦!" message:@"你敢拨打钓鱼热线: 400-636-6113" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"就打" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            
            NSString *phoneNum = cell.textLabel.text;
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]]];
        }];
        
        [alert addAction:sure];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"不打" style:UIAlertActionStyleCancel handler:NULL];
        
        [alert addAction:cancel];
        
        [self presentViewController:alert animated:YES completion:NULL];
        
    }

}

//设置cell
- (void)setCell:(UITableViewCell *)cell
{
    
    //添加textView
    UITextView *textView = [[UITextView alloc] init];
    
    self.textView = textView;
    
    textView.delegate = self;
    
    textView.font = [UIFont systemFontOfSize:15];
    
    //设置textView的背景颜色与tableView相同
    textView.backgroundColor = self.tableView.backgroundColor;
    
    [cell.contentView addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.contentView).offset(10);
        make.leading.equalTo(cell.contentView).offset(10);
        make.trailing.equalTo(cell.contentView).offset(-10);
        make.bottom.equalTo(cell.contentView).offset(-50);
    }];
    
    UILabel *placeholder = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, 200, 15)];
    
    self.placeholder = placeholder;
    
    placeholder.enabled = NO;
    
    placeholder.text = @"请输入反馈信息";
    
    placeholder.textColor = [UIColor lightGrayColor];
    
    placeholder.font = [UIFont systemFontOfSize:15];
    
    [textView addSubview:placeholder];
    
    //添加label
    UILabel *label = [[UILabel alloc] init];
    
    label.font = [UIFont systemFontOfSize:13];
    
    label.text = @"写反馈?😕你还能输入的字数:";
    
    [cell.contentView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textView.mas_bottom).offset(@5);
        make.leading.equalTo(cell.contentView).offset(@10);
        make.bottom.equalTo(cell.contentView).offset(@-5);
    }];
    
    //显示字数的label
    UILabel *numLabel = [[UILabel alloc] init];
    
    self.numLabel = numLabel;
    
    numLabel.font = [UIFont systemFontOfSize:13];
    
    numLabel.text = @"200";
    
    [cell.contentView addSubview:numLabel];
    
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(label.trailing).offset(5);
        make.top.equalTo(textView.mas_bottom).offset(@5);
        make.bottom.equalTo(cell.contentView).offset(@-5);
    }];
    
    //提交反馈按钮
    UIButton *button = [[UIButton alloc] init];
    
    [button setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    
    button.layer.cornerRadius = 5;
    
    button.layer.masksToBounds = YES;
    
    [button setTitle:@"提交反馈" forState:UIControlStateNormal];
    
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.contentView addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(cell.contentView).offset(@-10);
        make.top.equalTo(textView.mas_bottom).offset(@10);
        make.bottom.equalTo(cell.contentView).offset(@-10);
        make.width.equalTo(@70);
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return self.tableView.bounds.size.height * 0.4;
    }
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0.1;
    }
    return 20;
}

#pragma mark -- 提交反馈按钮点击事件
- (void)btnClick:(UIButton *)button
{
    if (self.textView.text.length == 0)
    {
        [SVProgressHUD showInfoWithStatus:@"请输入反馈信息"];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        [SVProgressHUD setFadeInAnimationDuration:0.5];
        
        [SVProgressHUD setFadeOutAnimationDuration:0.5];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }else if (self.textView.text.length > 200)
    {
        [SVProgressHUD showInfoWithStatus:@"不会数数吗?写那么多干嘛"];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        [SVProgressHUD setFadeInAnimationDuration:0.5];
        
        [SVProgressHUD setFadeOutAnimationDuration:0.5];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }else
    {
        [SVProgressHUD showSuccessWithStatus:@"反馈提交成功"];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        [SVProgressHUD setFadeInAnimationDuration:0.5];
        
        [SVProgressHUD setFadeOutAnimationDuration:0.5];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
            [SVProgressHUD dismiss];
        });
    }
}

#pragma mark --UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0)
    {
        self.placeholder.text = @"";
        
    }else
    {
        self.placeholder.text = @"请输入反馈信息";
    }
    
    NSInteger num = 200 - self.textView.text.length ;
    
    if (num >= 0)
    {
        self.numLabel.text = [NSString stringWithFormat:@"%lu",200 - self.textView.text.length];
    }else
    {
        NSMutableString *str = textView.text.mutableCopy;
        
        [str deleteCharactersInRange:NSMakeRange(textView.text.length - 1, 1)];
        
        textView.text = str;
        
        textView.userInteractionEnabled = NO;
        
        [SVProgressHUD showErrorWithStatus:@"字数超过限制"];
        
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        [SVProgressHUD setFadeInAnimationDuration:0.5];
        
        [SVProgressHUD setFadeOutAnimationDuration:0.5];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [SVProgressHUD dismiss];
            
            textView.userInteractionEnabled = YES;
            
        });
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
